import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../layout/Home.vue'
import Dash from '../views/Dashboard/index.vue'
import local from '@/utils/local.js'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    redirect: '/dashboard',
    children: [
      // 后台首页
      {
        path: '/dashboard',
        component: Dash
      },
      // 订单管理
      {
        path: '/order',
        meta: { name: '订单管理' },
        component: () =>
          import(/* webpackChunkName: "login" */ '../views/Order/index.vue')
      },
      // 商品管理
      {
        path: '/goods',
        meta: { name: '商品管理' },
        redirect: '/goods/list',
        component: () =>
          import(/* webpackChunkName: "login" */ '../views/Goods/index.vue'),
        children: [
          {
            path: '/goods/list',
            meta: { name: '商品列表' },
            component: () =>
              import(/* webpackChunkName: "login" */ '../views/Goods/list.vue')
          },
          {
            path: '/goods/add',
            meta: { name: '商品添加' },
            component: () =>
              import(/* webpackChunkName: "login" */ '../views/Goods/add.vue')
          },
          {
            path: '/goods/cate',
            meta: { name: '商品分类' },
            component: () =>
              import(/* webpackChunkName: "login" */ '../views/Goods/cate.vue')
          }
        ]
      },
      // 店铺管理
      {
        path: '/shop',
        component: () =>
          import(/* webpackChunkName: "login" */ '../views/Shop/index.vue')
      },
      // 账号管理
      {
        path: '/account',
        meta: { name: '账号管理' },
        redirect: '/account/list',
        component: () =>
          import(/* webpackChunkName: "login" */ '../views/Account/index.vue'),
        children: [
          {
            path: '/account/list',
            meta: { name: '账号列表' },
            component: () =>
              import(
                /* webpackChunkName: "login" */ '../views/Account/list.vue'
              )
          },
          {
            path: '/account/add',
            meta: { name: '添加账号' },
            component: () =>
              import(/* webpackChunkName: "login" */ '../views/Account/add.vue')
          },
          {
            path: '/account/reset',
            meta: { name: '修改密码' },
            component: () =>
              import(
                /* webpackChunkName: "login" */ '../views/Account/reset.vue'
              )
          },
          {
            path: '/account/person',
            meta: { name: '个人中心' },
            component: () =>
              import(
                /* webpackChunkName: "login" */ '../views/Account/Personal.vue'
              )
          }
        ]
      },
      // 销售统计
      {
        path: '/total',
        component: () =>
          import(/* webpackChunkName: "login" */ '../views/Total/index.vue'),
        children: [
          {
            path: '/total/goods',
            component: () =>
              import(/* webpackChunkName: "login" */ '../views/Total/goods.vue')
          },
          {
            path: '/total/orders',
            component: () =>
              import(
                /* webpackChunkName: "login" */ '../views/Total/orders.vue'
              )
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    component: () =>
      import(/* webpackChunkName: "login" */ '../views/Login.vue')
  }
]

// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')

const router = new VueRouter({
  routes
})

// 导航守卫,目的是不允许未登录的用户进入路由页面

router.beforeEach((to, from, next) => {
  const token = local.get('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
